<?php
namespace Admin\Controller;
use Think\Controller;
use Admin\Controller\CommonController;

class AppindexController extends CommonController {
    public function index(){
        $cate=M('FrontIndexCategory')->select();

        foreach($cate as $k=>$v){
            $res=M('FrontIndexPublish')->where(array('category_id'=>$v['tid']))->select();
            $cate[$k]['data']=array();
            foreach($res as $kk=>$vv){
                switch($vv['type']){
                    case 'live':
                        $res2=M('Live')->where(array('group_id'=>$vv['live_group_id']))->find();
                        $cate[$k]['item'][$kk]['text']=$res2['title'];
                        $cate[$k]['item'][$kk]['img']=$res2['image_url']?$res2['image_url']:'/Public/images/defaultpic.gif';
                        $cate[$k]['item'][$kk]['aid']=$vv['id'];
                        break;
                    case 'special':
                        $res2=M('FrontTheme')->find($vv['theme_id']);
                        $cate[$k]['item'][$kk]['text']=$res2['name'];
                        $cate[$k]['item'][$kk]['img']=$res2['image_url']?$res2['image_url']:'/Public/images/defaultpic.gif';
                        $cate[$k]['item'][$kk]['aid']=$vv['id'];
                        break;
                }
            }
        }

       $this->assign('cate',$cate);
       $this->display();
    }

    public function ajaxGetIndex(){
        $cate=M('FrontIndexCategory')->select();

        foreach($cate as $k=>$v){
            $res=M('FrontIndexPublish')->where(array('category_id'=>$v['tid']))->order('sort desc')->select();
            $cate[$k]['data']=array();
            foreach($res as $kk=>$vv){
                switch($vv['type']){
                    case 'live':
                        $res2=M('Live')->where(array('group_id'=>$vv['live_group_id']))->find();
                        $cate[$k]['item'][$kk]['text']=$res2['title'];
                        $cate[$k]['item'][$kk]['img']=$res2['image_url']?$res2['image_url']:'/Public/images/defaultpic.gif';
                        $cate[$k]['item'][$kk]['aid']=$vv['id'];
                        break;
                    case 'special':
                        $res2=M('FrontTheme')->find($vv['theme_id']);
                        $cate[$k]['item'][$kk]['text']=$res2['name'];
                        $cate[$k]['item'][$kk]['img']=$res2['image_url']?$res2['image_url']:'/Public/images/defaultpic.gif';
                        $cate[$k]['item'][$kk]['aid']=$vv['id'];
                        break;
                }
            }
        }
        $this->ajaxReturn($cate);
    }

    public function ajaxGetItems(){
        $tid=I('get.tid');
        $pagesize=10;

        $list1 = M('Publish')->where(array('category_id'=>$tid,'state'=>1))->order('`sort` DESC')->page($_GET['p'].','.$pagesize)->select();
        $list2 = M('Publish')->where(array('category_id'=>$tid,'state'=>1))->order('`sort` DESC')->page(($_GET['p']+1).','.$pagesize)->select();
        $list  = array_merge($list1,$list2);
        $count      = M('Publish')->where(array('category_id'=>$tid,'state'=>1))->count();


        $arr=array();
        $i=0;
        foreach($list as $k=>$v){
            if($i>=$pagesize){
                break;
            }
            $indexRes= M('FrontIndexPublish')->where(array('id'=>$v['id']))->find();
            if($indexRes){
                unset($list[$k]);
                $i--;
            }else{
                switch($v['type']){
                    case 'live':
                        $res2=M('Live')->where(array('group_id'=>$v['live_group_id']))->find();
                        $arr[$k]['text']=$res2['title'];
                        $arr[$k]['img']=$res2['image_url']?$res2['image_url']:'/Public/images/defaultpic.gif';
                        $arr[$k]['aid']=$v['id'];
                        break;
                    case 'special':
                        $res2=M('FrontTheme')->find($v['theme_id']);
                        $arr[$k]['text']=$res2['name'];
                        $arr[$k]['img']=$res2['image_url']?$res2['image_url']:'/Public/images/defaultpic.gif';
                        $arr[$k]['aid']=$v['id'];
                        break;
                }   
            }
            $i++;

        }

        $mess['data']=$arr;
        $mess['total']=(int) $count;
        $mess['pagesize']=$pagesize;

        $this->ajaxReturn($mess);
    }

    public function saveIndexPage(){

        if($_POST){
            M()->startTrans();
            foreach(I('post.indexPage') as $v){
                echo $res=M('FrontIndexPublish')->where(array('category_id'=>$v['tid']))->delete();
                $tItem=array();
                if($res === false){
                    echo 44444;
                    M()->rollback();

                }

                if($v['item']){

                    foreach($v['item'] as $k=>$vv){
                    		
                    		$tItem[$k]=M('Publish')->find($vv);
                    		if($vv){	
                           	 $tItem[$k]['sort']=count($v['item'])-$k;
                    		}else{
                    			unset($tItem[$k]);
                    		}
                            
                    }
                    echo $res2=M('FrontIndexPublish')->addAll($tItem);
                    if($res2===false){
                        echo '失败';
                        M()->rollback();
                        break;
                    }
                }
            }                    
            echo '成功';
            M()->commit();
        }else{
            echo 11111;
        }

    }

}